package com.qiwo.api.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

/**
 * Copyright
 *
 * @Author:chenwenchao
 * @Date:2018/10/11 0011
 * Description:
 */
public class AuthUtil {
    public static final String APPID = "wxd5a3b897aa3dd35b";
    public static final String APPSECRET = "689c4dc79479ebbadac4a5d70c704715";

    public static JSONObject doGetJson(String url) throws ClientProtocolException, IOException {
        JSONObject jsonObject = null;
        //首先初始化HttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //通过get方式进行提交
        HttpGet httpGet = new HttpGet(url);
        //通过HTTPclient的execute方法进行发送请求
        HttpResponse response = httpclient.execute(httpGet);
        //从response里面拿自己想要的结果
        HttpEntity entity = response.getEntity();
        if(entity != null){
            String result = EntityUtils.toString(entity,"UTF-8");
            jsonObject = JSON.parseObject(result);
        }
        //把链接释放掉
        httpGet.releaseConnection();
        return jsonObject;
    }

    public static String doGetString(String url) throws ClientProtocolException, IOException {
        String jsonObject = null;
        //首先初始化HttpClient对象
        CloseableHttpClient httpclient = HttpClients.createDefault();
        //通过get方式进行提交
        HttpGet httpGet = new HttpGet(url);
        //通过HTTPclient的execute方法进行发送请求
        HttpResponse response = httpclient.execute(httpGet);
        //从response里面拿自己想要的结果
        HttpEntity entity = response.getEntity();
        if(entity != null){
            String result = EntityUtils.toString(entity,"UTF-8");
            jsonObject = result;
        }
        //把链接释放掉
        httpGet.releaseConnection();
        return jsonObject;
    }
}
